package com.wangzeqi.changliaoba.messege;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.singleton.QueueSingleTon;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by dllo on 16/3/3.
 */
public class RecyclerViewOfMessegeAdapter extends RecyclerView.Adapter {
    //文本
    private static final int SEND_TEXT_TYPE = 1;
    private static final int RECEIVE_TEXT_TYPE = 2;
    //图片
    private static final int SEND_PICTURE_TYPE = 3;
    private static final int RECEIVE_PICTURE_TYPE = 4;
    //语音
    private static final int SEND_VOICE_TYPE = 5;
    private static final int RECEIVE_VOICE_TYPE = 6;

    private Context context;

    public void setMyClick(MyClick myClick) {
        this.myClick = myClick;
    }

    private MyClick myClick;
    private ArrayList<ChatInformations> chatInformationsData;

    public RecyclerViewOfMessegeAdapter(Context context, ArrayList<ChatInformations> chatInformationsData) {
        this.context = context;
        this.chatInformationsData = chatInformationsData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                View view1 = LayoutInflater.from(context).inflate(R.layout.item_chatlist_send, null);
                ViewHolderForSend viewHolderForSend = new ViewHolderForSend(view1);
                return viewHolderForSend;
            case 2:
                View view2 = LayoutInflater.from(context).inflate(R.layout.item_chatlist_receive, null);
                ViewHolderForReceive viewHolderForReceive = new ViewHolderForReceive(view2);
                return viewHolderForReceive;
            case 3:
                View view3 = LayoutInflater.from(context).inflate(R.layout.item_chatlist_send_picture, null);
                ViewHolderForSendPicture viewHolderForSendPicture = new ViewHolderForSendPicture(view3);
                return viewHolderForSendPicture;
            case 4:
                View view4 = LayoutInflater.from(context).inflate(R.layout.item_chatlist_receive_picture, null);
                ViewHolderForReceivePicture viewHolderForReceivePicture = new ViewHolderForReceivePicture(view4);
                return viewHolderForReceivePicture;
            case 5:
                View view5 = LayoutInflater.from(context).inflate(R.layout.item_chatlist_send_voice, null);
                ViewHolderForSendVoice viewHolderForSendVoice = new ViewHolderForSendVoice(view5);
                return viewHolderForSendVoice;
            case 6:
                View view6 = LayoutInflater.from(context).inflate(R.layout.item_chatlist_receive_voice,null);
                ViewHolderForReceiveVoice viewHolderForReceiveVoice = new ViewHolderForReceiveVoice(view6);
                return viewHolderForReceiveVoice;
            case 7:
                break;
            case 8:
                break;


        }
        return null;
    }
//        if (viewType == SEND_TEXT_TYPE) {
//
//        } else if (viewType == RECEIVE_TEXT_TYPE) {
//
//        }
//        return null;

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == SEND_TEXT_TYPE) {
            ((ViewHolderForSend) holder).textViewSend.setText(chatInformationsData.get(position).getContent());
            ((ViewHolderForSend) holder).itemView.findViewById(R.id.senditem_tv).setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    int index = holder.getLayoutPosition();
                    if (myClick != null) {
                        myClick.onClick(index, ((ViewHolderForSend) holder).textViewSend.getText().toString());
                    }

                    return true;
                }
            });
        } else if (getItemViewType(position) == RECEIVE_TEXT_TYPE) {
            ((ViewHolderForReceive) holder).textViewReceive.setText(chatInformationsData.get(position).getContent());
            ((ViewHolderForReceive) holder).itemView.findViewById(R.id.receiveitem_tv).setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int index = holder.getLayoutPosition();
                    if (myClick != null) {
                        myClick.onClick(index, ((ViewHolderForReceive) holder).textViewReceive.getText().toString());
                    }
                    return true;
                }
            });
        } else if (getItemViewType(position) == SEND_PICTURE_TYPE) {
            Bitmap bitmap = getSmallBitmap(chatInformationsData.get(position).getContent());
            ((ViewHolderForSendPicture) holder).imageViewSend.setImageBitmap(bitmap);


        } else if (getItemViewType(position) == RECEIVE_PICTURE_TYPE) {
            QueueSingleTon.getInstance().imgJson(((ViewHolderForReceivePicture) holder).imageViewReceive, chatInformationsData.get(position).getContent());

        } else if (getItemViewType(position) == SEND_VOICE_TYPE) {
            final MediaPlayer mediaPlayer = new MediaPlayer();
            final int[] state = {0};
            try {
                mediaPlayer.setDataSource(chatInformationsData.get(position).getContent());
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d("RecyclerViewOfMessegeAd", chatInformationsData.get(position).getContent());

            int time = mediaPlayer.getDuration() / 1000;
            ((ViewHolderForSendVoice) holder).textViewSendTime.setText(time + "''");
            ((ViewHolderForSendVoice) holder).itemView.findViewById(R.id.voice_rl).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = holder.getLayoutPosition();
                    if (state[0] == 0) {
                        mediaPlayer.start();
                    } else {
                        try {
                            mediaPlayer.reset();
                            mediaPlayer.setDataSource(chatInformationsData.get(index).getContent());
                            mediaPlayer.prepare();
                            mediaPlayer.start();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                    state[0] = 1;

                }
            });
        }else if(getItemViewType(position) == RECEIVE_VOICE_TYPE){
            final MediaPlayer mediaPlayer = new MediaPlayer();
            final int[] state = {0};
            try {
                mediaPlayer.setDataSource(chatInformationsData.get(position).getContent());
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d("RecyclerViewOfMessegeAd", chatInformationsData.get(position).getContent());

            int time = mediaPlayer.getDuration() / 1000;
            ((ViewHolderForReceiveVoice) holder).textViewReceiveTime.setText(time + "''");
            ((ViewHolderForReceiveVoice) holder).itemView.findViewById(R.id.receive_voice_rl).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = holder.getLayoutPosition();
                    if (state[0] == 0) {
                        mediaPlayer.start();
                    } else {
                        try {
                            mediaPlayer.reset();
                            mediaPlayer.setDataSource(chatInformationsData.get(index).getContent());
                            mediaPlayer.prepare();
                            mediaPlayer.start();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    state[0] = 1;

                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return chatInformationsData.size();
    }

    @Override
    public int getItemViewType(int position) {

        return chatInformationsData.get(position).getType();
    }


    //viewholder集

    //发送文本
    public class ViewHolderForSend extends RecyclerView.ViewHolder {
        private TextView textViewSend;

        public ViewHolderForSend(View itemView) {
            super(itemView);

            textViewSend = (TextView) itemView.findViewById(R.id.senditem_tv);
        }
    }

    //接受文本
    public class ViewHolderForReceive extends RecyclerView.ViewHolder {
        private TextView textViewReceive;

        public ViewHolderForReceive(View itemView) {
            super(itemView);

            textViewReceive = (TextView) itemView.findViewById(R.id.receiveitem_tv);
        }
    }

    //发送图片
    public class ViewHolderForSendPicture extends RecyclerView.ViewHolder {
        private ImageView imageViewSend;

        public ViewHolderForSendPicture(View itemView) {
            super(itemView);
            imageViewSend = (ImageView) itemView.findViewById(R.id.senditem_picture_iv);
        }
    }

    //接受图片
    public class ViewHolderForReceivePicture extends RecyclerView.ViewHolder {
        private NetworkImageView imageViewReceive;

        public ViewHolderForReceivePicture(View itemView) {
            super(itemView);
            imageViewReceive = (NetworkImageView) itemView.findViewById(R.id.receiveitem_picture_iv);
        }
    }

    //发送语音消息
    public class ViewHolderForSendVoice extends RecyclerView.ViewHolder {
        private RelativeLayout relativeLayoutSend;
        private TextView textViewSendTime;
        private ImageView imageViewDian;

        public ViewHolderForSendVoice(View itemView) {
            super(itemView);
            relativeLayoutSend = (RelativeLayout) itemView.findViewById(R.id.voice_rl);
            textViewSendTime = (TextView) itemView.findViewById(R.id.voice_tv_time);
            imageViewDian = (ImageView) itemView.findViewById(R.id.voice_iv_dian);
        }
    }
    //接受语音消息
    public class ViewHolderForReceiveVoice extends RecyclerView.ViewHolder{
        private TextView textViewReceiveTime;
        public ViewHolderForReceiveVoice(View itemView) {
            super(itemView);
            textViewReceiveTime = (TextView) itemView.findViewById(R.id.receive_voice_tv_time);
        }
    }


    //压缩bitmap图片方法
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 70, 70);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

}
